package com.wanpinghui.supplier.user;

import android.content.Intent;
import android.net.Uri;
import android.os.Looper;
import android.provider.Settings;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wanpinghui.supplier.BuildConfig;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.message.nim.NimManager;
import com.wanpinghui.supplier.oauth.LoginActivity;
import com.wanpinghui.supplier.utils.ActivityJumpUtil;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.oauth.OAuthService;
import com.wanpinghui.wphlibrary.widgets.SwitchView;
import com.yunshl.yunshllibrary.utils.SystemUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

/**
 * 功能描述
 * author:caitiangui
 * time:2017/11/27
 */
@ContentView(R.layout.layout_activity_setting)
public class SettingActivity extends BaseActivity {

    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser mLayoutTitle;
    @ViewInject(R.id.fl_about)
    private FrameLayout mAboutFl;
    @ViewInject(R.id.tv_logout)
    private TextView mTextViewLogout;
    @ViewInject(R.id.switch_view)
    private SwitchView switchView;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        switchView.setCanNotAutoOpen(true);
    }

    @Override
    public void bindEvents() {
        mLayoutTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mAboutFl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this, AboutActivity.class));
            }
        });

        mTextViewLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WPHSdk.getService(OAuthService.class).logout();
                NimManager.getInstance().logout();
                clearImageDiskCache();
                Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });

        switchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri packageURI = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
                startActivity(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI));
            }
        });
    }

    @Override
    public void initData() {

    }


    /**
     * 清除图片磁盘缓存
     */
    public void clearImageDiskCache() {
        try {
            if (Looper.myLooper() == Looper.getMainLooper()) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.get(getApplicationContext()).clearDiskCache();
                    }
                }).start();
            } else {
                Glide.get(getApplicationContext()).clearDiskCache();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (SystemUtil.isNotificationEnabled(this)) {
            switchView.setOpened(true);
            NimManager.getInstance().enableNotification(true);
        } else {
            switchView.setOpened(false);
            NimManager.getInstance().enableNotification(false);
        }
    }
}
